package com.ruoyi.ax.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.ax.domain.AxAccountChangeRecord;
import com.ruoyi.ax.domain.AxAuthorizationSell;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.changeSouceType;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.sign.Md5Utils;
import com.ruoyi.common.utils.wx.HttpClientSslUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ax.domain.AxRechargeRecord;
import com.ruoyi.ax.service.IAxRechargeRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 充值记录Controller
 * 
 * @author ruoyi
 * @date 2025-05-07
 */
@RestController
@RequestMapping("/ax/axRechargeRecord")
public class AxRechargeRecordController extends BaseController
{
    @Autowired
    private IAxRechargeRecordService axRechargeRecordService;

    /**
     * 查询充值记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AxRechargeRecord axRechargeRecord)
    {
        startPage();
        List<AxRechargeRecord> list = axRechargeRecordService.selectAxRechargeRecordList(axRechargeRecord);
        return getDataTable(list);
    }

    /**
     * 导出充值记录列表
     */
    @Log(title = "充值记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AxRechargeRecord axRechargeRecord)
    {
        List<AxRechargeRecord> list = axRechargeRecordService.selectAxRechargeRecordList(axRechargeRecord);
        for(AxRechargeRecord axRechargeRecord1:list){
            SysUser sysUser = axRechargeRecord1.getSysUser();
            BeanUtils.copyProperties(sysUser,axRechargeRecord1);
        }
        ExcelUtil<AxRechargeRecord> util = new ExcelUtil<AxRechargeRecord>(AxRechargeRecord.class);
        util.exportExcel(response, list, "充值记录数据");
    }

    @Log(title = "用户充值", businessType = BusinessType.OTHER)
    @PostMapping("/userRecharge")
    public AjaxResult userRecharge(@RequestBody  AxRechargeRecord axRechargeRecord)
    {

        return  toAjax(axRechargeRecordService.userRecharge(axRechargeRecord));

    }

    @Log(title = "用户充值", businessType = BusinessType.OTHER)
    @PostMapping("/pcUserRecharge")
    public AjaxResult pcUserRecharge(@RequestBody  AxRechargeRecord axRechargeRecord)
    {

        return  toAjax(axRechargeRecordService.pcUserRecharge(axRechargeRecord));

    }


    @Log(title = "用户充值获取支付地址", businessType = BusinessType.OTHER)
    @GetMapping("/toRechargezfb")
    public AjaxResult toRechargezfb( AxRechargeRecord axRechargeRecord)
    {
        return  success(axRechargeRecordService.getZfbPaySign(axRechargeRecord));
    }




    @Log(title = "用户充值获取支付地址", businessType = BusinessType.OTHER)
    @GetMapping("/toRecharge")
    public AjaxResult toRecharge( AxRechargeRecord axRechargeRecord)
    {
        return  success(axRechargeRecordService.getWxPaySign(axRechargeRecord));
    }


    /**
     * 小程序回调
     */
    @PostMapping("/wxPayNotify")
    @Log(title = "充值微信支付回调", businessType = BusinessType.OTHER)
    public  String payNotify(HttpServletRequest request, HttpServletResponse response)
    {
        //注意：回调接口需要暴露到公网上，且要放开token验证
        //  Map<String, Object> map = zyValueCardOrderService.payNotify(request, response);
        axRechargeRecordService.rechargeWxPayNotify(request, response);
        return "SUCCESS";

    }


    /**
     * 获取充值记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(axRechargeRecordService.selectAxRechargeRecordById(id));
    }

    /**
     * 新增充值记录
     */
    @Log(title = "新增充值记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AxRechargeRecord axRechargeRecord)
    {
        return toAjax(axRechargeRecordService.insertAxRechargeRecord(axRechargeRecord));
    }

    /**
     * 修改充值记录
     */
    @Log(title = "修改充值记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AxRechargeRecord axRechargeRecord)
    {
        return toAjax(axRechargeRecordService.updateAxRechargeRecord(axRechargeRecord));
    }

    /**
     * 删除充值记录
     */
    @Log(title = "删除充值记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(axRechargeRecordService.deleteAxRechargeRecordByIds(ids));
    }


}
